#include "precomp.h"
#include "EmpireLuaFunctions.h"

#include "../Entity/Modules/EntityBuilding.h"

namespace Tmpl {

// print function
static int LuaPrintString(lua_State *L) 
{
	// get number of arguments
	int n = lua_gettop(L);

	// loop through each argument
	char txt[8192]; txt[0] = 0;
	for (int i = 1; i <= n; i++)
	{
		// Error checking
		if (!lua_isstring(L, i))
		{
			lua_pushstring(L, "Incorrect argument to 'print'");
			lua_error(L);
		}
		else if (i > 1)
		{
			sprintf_s(txt, "%s\t%s", txt, lua_tostring(L, i));
		}
		else
		{
			sprintf_s(txt, "%s", lua_tostring(L, i));
		}
	}
	if (txt[0]) Log::Write(txt);

	// no returns
	return 0;
}
// building collision functions
static int LuaSetBuildingCollisionSize(lua_State *L) 
{
	// get number of arguments
	int n = lua_gettop(L);
	if (n != 3)
	{ 
		lua_pushstring(L, "Incorrect number arguments in SetBuildingCollisionSize, expected 3");
		lua_error(L);
	}
	else
	{
		// validate
		for (int i=1; i<=3; ++i)
		{
			if (!lua_isnumber(L, i))
			{
				lua_pushstring(L, "Incorrect argument types in SetBuildingCollisionSize expected 3 integers");
				lua_error(L);
			}
		}

		// set the collision size
		EntityBuilding* building = EntityBuildingSystem::GetBuilding(lua_tointeger(L, 1));
		building->SetCollisionSize(lua_tointeger(L, 2), lua_tointeger(L, 3));
	}

	return 0;
}

static int LuaSetBuildingCollision(lua_State *L) 
{
	// get number of arguments
	int n = lua_gettop(L);
	if (n != 3)
	{ 
		lua_pushstring(L, "Incorrect number arguments in AddBuildingCollision, expected 3");
		lua_error(L);
	}
	else
	{
		// validate
		for (int i=1; i<=3; ++i)
		{
			if (!lua_isnumber(L, i))
			{
				lua_pushstring(L, "Incorrect arguments expected 3 integers");
				lua_error(L);
			}
		}

		// add the collision
		EntityBuilding* building = EntityBuildingSystem::GetBuilding(lua_tointeger(L, 1));
		building->GetCollision()[lua_tointeger(L, 3) * building->GetCollisionSizeX() + lua_tointeger(L, 2)] = true; 
	}

	return 0;
}





// Register function
void LuaRegisterEmpireFunctions(lua_State* L)
{
	lua_register(L, "SetBuildingCollisionSize", LuaSetBuildingCollisionSize);
	lua_register(L, "SetBuildingCollision", LuaSetBuildingCollision);
	lua_register(L, "print", LuaPrintString);
}
// Misc
const char* LuaTableGetString(lua_State* L, const char* name, int idx)
{
	lua_pushstring(L, name);
	lua_gettable(L, idx);   // This pops "name", gets the value of "name" in the table and pushes the value
    if(!lua_isstring(L,-1))
	{
		//Tmpl::Log::Print("Error in LuaTableGetString, name = %s: %s\n", name, lua_tostring(L, -1));
		lua_pop(L, 1);
		return "";
	}
	const char* value = lua_tostring(L, -1);   // Get the returned value off the stack
	lua_pop(L, 1);   // Pops that value off the stack (returning stack to its original state)
	return value;
}
double LuaTableGetNumber(lua_State* L, const char* name, int idx)
{
	lua_pushstring(L, name);
	lua_gettable(L, idx);   // This pops "name", gets the value of "name" in the table and pushes the value
    if(!lua_isnumber(L,-1))
	{
		//Tmpl::Log::Print("Error in LuaTableGetNumber, name = %s: %s\n", name, lua_tostring(L, -1));
		lua_pop(L, 1);
		return 0;
	}
	double number = lua_tonumber(L, -1);   // Get the returned value off the stack
	lua_pop(L, 1);   // Pops that value off the stack (returning stack to its original state)
	return number;
}
}